/**
 * 
 */
package com.platform.template.redis;

import javax.annotation.PostConstruct;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import redis.clients.jedis.JedisPoolConfig;

import com.platform.common.utils.Assert;

/**
 * <pre>
 * 功能：Redis的工具类
 * 
 * &lt;bean id="redisTemplate" class="com.platform.template.redis.MyRedisTemplate"&gt;
 * 		&lt;property name="hostName" value="${redis.hostName}" /&gt;
 * 		&lt;property name="port" value="${redis.port}" /&gt;
 * 		&lt;property name="database" value="${redis.database}" /&gt;
 * 		&lt;property name="password" value="${redis.password}" /&gt;
 * &lt;/bean&gt;
 * 
 * 在properties文件中配置：
 * redis.hostName
 * redis.port
 * redis.database
 * redis.password
 * 
 * </pre>
 * @author wei.gao
 */
public class MyRedisTemplate<K, V> {
	private int maxIdle = 200;
	private int maxTotal = 1000;
	private int maxWaitMillis = 10000;
	private int timeout = 3000;
	private String hostName;
	private int port;
	private int database;
	private String password;
	
	@PostConstruct
	private void init(){
		Assert.notEmpty("hostName", hostName);
		Assert.notEmpty("port", port);
		Assert.notEmpty("database", database);
		Assert.notEmpty("password", password);
	}
	
	/**
	 * <pre>
	 * 功能：初始化连接池
	 * </pre>
	 * @return
	 */
	private JedisPoolConfig getPoolConfig(){
		JedisPoolConfig pool = new JedisPoolConfig();
		pool.setMaxIdle(maxIdle);
		pool.setMaxTotal(maxTotal);
		pool.setBlockWhenExhausted(true);
		pool.setMaxWaitMillis(maxWaitMillis);
		pool.setTestOnBorrow(true);
		return pool;
	}
	
	/**
	 * <pre>
	 * 功能：初始化连接工厂
	 * </pre>
	 * @return
	 */
	private JedisConnectionFactory getConnectionFactory(){
		JedisConnectionFactory factory = new JedisConnectionFactory();
		factory.setUsePool(true);
		factory.setPoolConfig(getPoolConfig());
		factory.setHostName(hostName);
		factory.setPort(port);
		factory.setDatabase(database);
		factory.setPassword(password);
		factory.setTimeout(timeout);
		return factory;
	}
	
	/**
	 * <pre>
	 * 功能：获取操作对象
	 * </pre>
	 * @return
	 */
	public RedisTemplate<K, V> getTemplate(){
		RedisTemplate<K, V> template = new RedisTemplate<K, V>();
		template.setConnectionFactory(getConnectionFactory());
		template.setEnableTransactionSupport(true);
		template.setValueSerializer(new JsonStringRedisSerializer());
		template.setHashValueSerializer(new JsonStringRedisSerializer());
		return template;
	}

	public int getMaxIdle() {
		return maxIdle;
	}

	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}

	public int getMaxTotal() {
		return maxTotal;
	}

	public void setMaxTotal(int maxTotal) {
		this.maxTotal = maxTotal;
	}

	public int getMaxWaitMillis() {
		return maxWaitMillis;
	}

	public void setMaxWaitMillis(int maxWaitMillis) {
		this.maxWaitMillis = maxWaitMillis;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public String getHostName() {
		return hostName;
	}

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getDatabase() {
		return database;
	}

	public void setDatabase(int database) {
		this.database = database;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}
